package com.ycm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ycm.core.Result;
import com.ycm.dto.UserPageQueryDto;
import com.ycm.dto.UserSaveDto;
import com.ycm.service.UserService;
import com.ycm.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author ycm
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getUser")
    @Operation(summary = "查询用户")
    public Result<UserVo> getUser(@RequestParam String id) {
        UserVo user = userService.getUserById(id);
        return Result.success(user);
    }

    @GetMapping("/getUserByName")
    @Operation(summary = "查询用户")
    public Result<UserVo> getUserByName(@RequestParam String name) {
        UserVo user = userService.getUserByName(name);
        return Result.success(user);
    }

    @PostMapping("/getPageList")
    @Operation(summary = "分页查询用户")
    public Result<IPage<UserVo>> getPageList(@RequestBody @Valid UserPageQueryDto dto) {
        return Result.success(userService.getPageList(dto));
    }

    @PostMapping("/saveUser")
    @Operation(summary = "保存用户")
    public Result<?> saveUser(@RequestBody @Valid UserSaveDto dto) {
        userService.saveUser(dto);
        return Result.success();
    }

    @GetMapping("/deleteById")
    @Operation(summary = "删除用户")
    public Result<?> deleteById(@RequestParam String id) {
        userService.deleteById(id);
        return Result.success();
    }
}
